package com.example.activities;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.example.R;
import com.example.models.UserSingleton;
import com.example.models.managers.Loginer;

public class Main extends Activity implements View.OnClickListener {
    private Button loginButton;
    private Button registrationButton;
    private EditText loginText;
    private EditText passwordText;
    private ProgressDialog progressDialog;
    private boolean isLogin = false;
    private final int ALERT_DIALOG_ONE = 0x98;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        setContentView(R.layout.main);

        loginButton = (Button) findViewById(R.id.logFormButtonLogin);
        registrationButton = (Button) findViewById(R.id.logFormButtonRegistered);
        loginText = (EditText) findViewById(R.id.logFormEditLogin);
        passwordText = (EditText) findViewById(R.id.logFormEditPassword);
        progressDialog = new ProgressDialog(this);

        loginButton.setOnClickListener(this);
        registrationButton.setOnClickListener(this);
        loginText.setText("nenekirimaru");
        passwordText.setText("art2378181");

    }

    /*
    TODO Registration user action
     */
    private void onRegistered() {
        Intent i = new Intent(Main.this, Registration.class);
        startActivity(i);
    }

    private void onLogin() {
        Loginer loginer = new Loginer();
        loginer.setUsername(loginText.getText().toString().toLowerCase().trim());
        loginer.setPassword(passwordText.getText().toString().trim());
        try {
            UserSingleton.getInstance().setUser(loginer.getCurrentUser());
            UserSingleton.getInstance().setTopics(loginer.getTopics());
            Log.e("MESSAGE", UserSingleton.getInstance().getUser().getUsername());
            isLogin = true;
        } catch (Exception e) {
            Log.e("CONNECTION ERROR", "LOG", e);
            try {
                loginer.closeConnection();
            } catch (Exception e1) {
            }
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.logFormButtonLogin: {
                BackgroundLoginer backgroundLoginer = new BackgroundLoginer();
                backgroundLoginer.execute();
                break;
            }
            case R.id.logFormButtonRegistered: {
                onRegistered();
                break;
            }
            default:
                break;
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        UserSingleton.getInstance().clearUser();
        isLogin = false;
    }

    /*
    TODO Create error loging dialog
     */
    protected Dialog onCreateDialog(int id) {
        Dialog dialog;
        AlertDialog.Builder builder;
        switch (id) {
            case ALERT_DIALOG_ONE:
                builder = new AlertDialog.Builder(this);
                builder.setMessage("Login error! Please check login parameters...");
                builder.setCancelable(false);
                builder.setPositiveButton("OK", null);
                dialog = builder.create();
                break;
            default:
                dialog = null;
        }
        return dialog;
    }

    /*
     TODO Login progress
     */
    private class BackgroundLoginer extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = ProgressDialog.show(Main.this, "Authorize", "Please wait...",
                    true, false);
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            Intent i = new Intent(Main.this, UserCabinet.class);
            progressDialog.dismiss();
            if (isLogin) {
                startActivity(i);
            } else {
                showDialog(ALERT_DIALOG_ONE);
            }
        }

        @Override
        protected Void doInBackground(Void... voids) {
            onLogin();
            return null;
        }
    }

}
